package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.BankAdapter;
import com.eeepay.eeepay_v2.model.BankInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.ui.ABBaseActivity;

import java.util.List;

/**
 * Created by Ching on 2016/8/22.
 * 选择支行界面
 */
public class SelectBankActivity extends ABBaseActivity implements AdapterView.OnItemClickListener {

    private EditText branchEt;
    private ListView listView;

    private BankAdapter adapter;
    private List<BankInfo> bankList;
    private BankInfo bankInfo;
    private Intent intent;
//    private String[] bankList = new String[]{"深圳市朗山支行", "深圳市沙井支行", "深圳市龙华支行", "深圳市福田支行"};

    @Override

    protected int getLayoutId() {
        return R.layout.activity_select_branch;
    }

    @Override
    protected void initWidget() {
        branchEt = getViewById(R.id.et_zh);
        listView = getViewById(R.id.lv_select_branch);
        Log.d("bank", "SelectBankActivity");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null)
            bankList = (List<BankInfo>) bundle.getSerializable(Constant.KEY_LIST);
    }

    @Override
    protected void eventOnClick() {

//        listView.setTextFilterEnabled(true);//设置lv可以被过虑
        adapter = new BankAdapter(this);

        if (bankList != null && bankList.size() > 0) {
            adapter.setAllList(bankList);
            adapter.addAll(bankList);
            listView.setAdapter(adapter);
        }
        listView.setOnItemClickListener(this);

        // 添加EditText控件输入监听
        branchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    adapter.getFilter().filter(s);
                } else {
                    adapter.getFilter().filter("");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        bankInfo = (BankInfo) parent.getItemAtPosition(position);
//        branchEt.setText(bankInfo.getBank_name().toString());
        adapter.getFilter().filter("*");// 选择之后之后，情况list
        intent = new Intent();
        intent.putExtra(Constant.TAG, bankInfo);
        setResult(Constant.RESULTCODE_100, intent);
        finish();
    }
}
